home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicDesktopIconUI.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  255 lines

  1. /*  
  2.  * @(#)BasicDesktopIconUI.java    1.20 98/02/06
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.border.*;
  27. import com.sun.java.swing.plaf.*;
  28. import java.beans.*;
  29. import java.util.EventListener;
  30. import java.io.Serializable;
  31.  
  32.  
  33. /**
  34.  * Basic L&F for a minimized window on a desktop.
  35.  * <p>
  36.  * Warning: serialized objects of this class will not be compatible with
  37.  * future swing releases.  The current serialization support is appropriate 
  38.  * for short term storage or RMI between Swing1.0 applications.  It will
  39.  * not be possible to load serialized Swing1.0 objects with future releases
  40.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  41.  * baseline for the serialized form of Swing objects.
  42.  *
  43.  * @version 1.20 02/06/98
  44.  * @author David Kloba
  45.  * @author Steve Wilson
  46.  */
  47. public class BasicDesktopIconUI extends DesktopIconUI implements Serializable
  48. {
  49.     JInternalFrame.JDesktopIcon desktopIcon;
  50.     JComponent   iconPane;
  51.     EventListener mml;
  52.     JInternalFrame frame;
  53.  
  54.  
  55.     public static ComponentUI createUI(JComponent c)    {
  56.         return new BasicDesktopIconUI();
  57.     }
  58.  
  59.     public BasicDesktopIconUI() {
  60.     }
  61.  
  62.     public void installUI(JComponent c)   {
  63.     desktopIcon = (JInternalFrame.JDesktopIcon)c;
  64.     frame = desktopIcon.getInternalFrame();
  65.     installDefaults( desktopIcon );
  66.     installComponents(desktopIcon);
  67.     installListeners(desktopIcon);
  68.     JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
  69.     }
  70.  
  71.     public void uninstallUI(JComponent c) {
  72.     installDefaults( desktopIcon );
  73.     uninstallComponents(desktopIcon);
  74.     uninstallListeners(desktopIcon);
  75.     desktopIcon = null;
  76.     frame = null;
  77.     }
  78.  
  79.     protected void installComponents(JInternalFrame.JDesktopIcon dIcon) {
  80.     frame = dIcon.getInternalFrame();
  81.     iconPane = new BasicInternalFrameTitlePane(frame);
  82.     dIcon.setLayout(new BorderLayout());
  83.     dIcon.add(iconPane, BorderLayout.CENTER);
  84.     }
  85.  
  86.     protected void uninstallComponents(JInternalFrame.JDesktopIcon dIcon) {
  87.     dIcon.setLayout(null);
  88.     dIcon.remove(iconPane);
  89.     }
  90.  
  91.     protected void installListeners(JInternalFrame.JDesktopIcon dIcon) {
  92.     mml = createMotionListener(desktopIcon);
  93.     dIcon.addMouseMotionListener((MouseMotionListener)mml);
  94.     dIcon.addMouseListener((MouseListener)mml);
  95.     }
  96.  
  97.     protected void uninstallListeners(JInternalFrame.JDesktopIcon dIcon) {
  98.     dIcon.removeMouseMotionListener((MouseMotionListener)mml);
  99.     dIcon.removeMouseListener((MouseListener)mml);
  100.     }
  101.  
  102.     protected void installDefaults(JInternalFrame.JDesktopIcon dIcon) {
  103.         LookAndFeel.installBorder(dIcon, "DesktopIcon.border");
  104.     }
  105.  
  106.     protected void uninstallDefaults(JInternalFrame.JDesktopIcon dIcon) {
  107.     }
  108.  
  109.     protected MotionListener createMotionListener(JInternalFrame.JDesktopIcon dIcon) {
  110.         return new MotionListener();
  111.     }
  112.     
  113.     public Dimension getPreferredSize(JComponent c) {
  114.         JInternalFrame iframe = desktopIcon.getInternalFrame();
  115.     Border border = iframe.getBorder();
  116.         int w2 = 157;
  117.     int h2 = 18;
  118.  
  119.     if(border != null)
  120.         h2 += border.getBorderInsets(iframe).bottom + 
  121.                   border.getBorderInsets(iframe).top;
  122.  
  123.     return new Dimension(w2, h2);
  124.     }
  125.  
  126.     public Dimension getMinimumSize(JComponent c) {
  127.     return iconPane.getMinimumSize();
  128.     } 
  129.  
  130.     public Dimension getMaximumSize(JComponent c){
  131.     return iconPane.getMaximumSize();
  132.     }
  133.  
  134.     public Insets getInsets(JComponent c) {
  135.         JInternalFrame iframe = desktopIcon.getInternalFrame();
  136.     Border border = iframe.getBorder();
  137.     if(border != null)
  138.         return border.getBorderInsets(iframe);
  139.     
  140.     return new Insets(0,0,0,0);
  141.     }
  142.  
  143.     public void deiconize() {
  144.         try { frame.setIcon(false); } catch (PropertyVetoException e2) { }
  145.     }
  146.  
  147.     /**
  148.      * Listens for mouse movements and acts on them.
  149.      * <p>
  150.      * Warning: serialized objects of this class will not be compatible with
  151.      * future swing releases.  The current serialization support is appropriate
  152.      * for short term storage or RMI between Swing1.0 applications.  It will
  153.      * not be possible to load serialized Swing1.0 objects with future releases
  154.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  155.      * baseline for the serialized form of Swing objects.
  156.      */
  157.     public class MotionListener
  158.         extends MouseAdapter implements MouseMotionListener, Serializable
  159.     {
  160.     // _x & _y are the mousePressed location in absolute coordinate system
  161.         int _x, _y;
  162.     // __x & __y are the mousePressed location in source view's coordinate system
  163.     int __x, __y;
  164.         Rectangle startingBounds;
  165.  
  166.         public void mouseReleased(MouseEvent e) {
  167.             _x = 0;
  168.             _y = 0;
  169.             __x = 0;
  170.             __y = 0;
  171.             startingBounds = null;
  172.         }
  173.                 
  174.         public void mousePressed(MouseEvent e) {
  175.             Point p = SwingUtilities.convertPoint((Component)e.getSource(), 
  176.                         e.getX(), e.getY(), null);
  177.             __x = e.getX();
  178.             __y = e.getY();
  179.             _x = p.x;
  180.             _y = p.y;
  181.             startingBounds = desktopIcon.getBounds();
  182.  
  183.             try { frame.setSelected(true); } catch (PropertyVetoException e1) { }
  184.         if(desktopIcon.getParent() instanceof JLayeredPane) {
  185.         ((JLayeredPane)desktopIcon.getParent()).moveToFront(desktopIcon);
  186.          }
  187.  
  188.             if(e.getClickCount() > 1) {
  189.         if(frame.isIconifiable() && frame.isIcon()) {
  190.                     deiconize();
  191.         }
  192.             }
  193.  
  194.      }
  195.  
  196.          public void mouseMoved(MouseEvent e) {}
  197.  
  198.          public void mouseDragged(MouseEvent e) {   
  199.             Point p; 
  200.         int newX, newY, newW, newH;
  201.             int deltaX;
  202.             int deltaY;
  203.         Dimension min;
  204.         Dimension max;
  205.             p = SwingUtilities.convertPoint((Component)e.getSource(), 
  206.                                         e.getX(), e.getY(), null);
  207.         
  208.         Insets i = desktopIcon.getInsets();
  209.         int pWidth, pHeight;
  210.         pWidth = ((JComponent)desktopIcon.getParent()).getWidth();
  211.         pHeight = ((JComponent)desktopIcon.getParent()).getHeight();
  212.         
  213.         if (startingBounds == null) {
  214.           // (STEVE) Yucky work around for bug ID 4106552
  215.             return;
  216.         }
  217.         newX = startingBounds.x - (_x - p.x);
  218.         newY = startingBounds.y - (_y - p.y);
  219.         // Make sure we stay in-bounds
  220.         if(newX + i.left <= -__x)
  221.             newX = -__x - i.left;
  222.         if(newY + i.top <= -__y)
  223.             newY = -__y - i.top;
  224.         if(newX + __x + i.right > pWidth)
  225.             newX = pWidth - __x - i.right;
  226.         if(newY + __y + i.bottom > pHeight)
  227.             newY =  pHeight - __y - i.bottom;
  228.         
  229.         JDesktopPane d;
  230.         if((d = desktopIcon.getDesktopPane()) != null) {
  231.             DesktopManager dm;
  232.             dm = d.getDesktopManager();
  233.             dm.setBoundsForFrame(desktopIcon, newX, newY, 
  234.                          desktopIcon.getWidth(), 
  235.                          desktopIcon.getHeight());
  236.         } else {
  237.             moveAndRepaint(desktopIcon, newX, newY, 
  238.                 desktopIcon.getWidth(), desktopIcon.getHeight());
  239.         }
  240.         return;
  241.     }
  242.  
  243.         public void moveAndRepaint(JComponent f, int newX, int newY, 
  244.                     int newWidth, int newHeight) {
  245.         Rectangle r = f.getBounds();
  246.         f.setBounds(newX, newY, newWidth, newHeight);        
  247.         SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
  248.         f.getParent().repaint(r.x, r.y, r.width, r.height);
  249.         }    
  250.     }; /// End MotionListener
  251.  
  252. }
  253.  
  254.  
  255.